<!-- 编辑弹窗 -->
<template>
  <div class="ele-body">
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :destroy-on-close="true"
      :lock-scroll="false"
      title="批量删除关键词"
      :visible="visible"
      class="ele-popup-dialog"
      custom-class="ele-dialog-form"
      width="760px"
      @update:visible="updateVisible"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="86px">
        <el-form-item label="关键词:" prop="keywords">
          <el-input
            v-model="form.keywords"
            :rows="16"
            placeholder="请粘贴要删除的关键词一行一个."
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="updateVisible(false)">取消</el-button>
        <el-button :loading="loading" type="primary" @click="save"
          >确定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "xhsKeywordsDelEdit",
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object,
  },
  data() {
    return {
      // 表单数据
      form: Object.assign(
        {},
        {
          keywords: "",
        },
        this.data
      ),
      // 表单验证规则
      rules: {
        keywords: [
          {
            required: true,
            message: "请输入要删除的关键词",
            trigger: "blur",
          },
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
    };
  },
  watch: {
    data() {
      if (this.data.id) {
        this.form = Object.assign(
          {},
          {
            keywords: "",
          },
          this.data
        );
        this.isUpdate = true;
      } else {
        for (let i in this.form) {
          this.form[i] = null;
        }
        this.isUpdate = false;
      }
    },
  },
  methods: {
    /* 确定编辑 */
    save() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.$http
            .post("/Xhskeywords/dels", this.form)
            .then((res) => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message({ type: "success", message: res.data.msg });
                if (!this.isUpdate) {
                  this.form = Object.assign(
                    {},
                    {
                      keywords: "",
                    }
                  );
                }
                this.updateVisible(false);
                this.$emit("done");
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        } else {
          return false;
        }
      });
    },

    /* 更新visible */
    updateVisible(value) {
      this.$emit("update:visible", value);
    },
  },
};
</script>

<style scoped>
.ele-popup-dialog /deep/ .el-dialog__body {
  padding-top: 10px !important;
}
</style>
